package com.dragon.client.controller.finance;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.client.config.annotation.AddPage;
import com.dragon.client.config.annotation.Log;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.finance.IPlanCollectService;
import com.dragon.repository.domain.dto.PlanCollectDTO;
import com.dragon.repository.domain.vo.CustomerPaymentVO;
import com.dragon.repository.domain.vo.PlanCollectVO;
import com.dragon.repository.entity.CustomerPayment;
import com.dragon.repository.entity.PlanCollect;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 计划回款表 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-06-30
 */
@RestController
@RequestMapping("plancollect")
@Api(tags = "收款计划管理")
public class PlanCollectController {

    @Autowired
    IPlanCollectService service;

    @GetMapping("list")
    @ApiOperation("计划收款列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = true, type = "int"),
            @ApiImplicitParam(name = "search", value = "搜索条件", required = false, type = "string"),
            @ApiImplicitParam(name = "customerId", value = "客户id", required = false, type = "int"),
            @ApiImplicitParam(name = "planStatus", value = "完成状态", required = false, type = "int"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = false, type = "string"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, type = "string")
    })
    public ResponseData<List<PlanCollectDTO>> planList(@RequestParam Integer page,
                                                       @RequestParam Integer limit,
                                                       @RequestParam(required = false) String search,
                                                       @RequestParam(required = false) Integer customerId,
                                                       @RequestParam(required = false) Integer planStatus,
                                                       @RequestParam(required = false) String startTime,
                                                       @RequestParam(required = false) String endTime){
        Page<PlanCollect> planList = service.planList(page, limit, search, customerId, planStatus, startTime, endTime);
        return ResponseData.success(PlanCollectDTO.convertList(planList.getRecords()), planList.getTotal());
    }

    @PostMapping("add")
    @ApiOperation("添加计划收款")
    @Log
    public ResponseData addPlan(@RequestBody @Validated(Insert.class) PlanCollectVO vo){
        return ResponseData.success(service.addPlan(PlanCollectVO.convert(vo)));
    }

    @PostMapping("update")
    @ApiOperation("更新计划收款")
    @Log
    public ResponseData updatePlan(@RequestBody @Validated(Update.class) PlanCollectVO vo){
        service.updatePlan(PlanCollectVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("delete")
    @ApiOperation("删除计划收款")
    public ResponseData deletePlan(@ApiParam(name = "id", value = "计划回款id",required = true, type = "int") @RequestParam Integer id){
        service.deletePlan(id);
        return ResponseData.success();
    }

    @PostMapping("updateStatus")
    @ApiOperation(value = "单笔收款")
    //@AddPage
    public ResponseData updateStatusPlan(@RequestBody @Validated(Update.class) CustomerPaymentVO vo){
        service.updateStatus(CustomerPaymentVO.convert(vo));
        return ResponseData.success();
    }

    @GetMapping("statistics")
    @ApiOperation("财务统计信息")
    public ResponseData businessStatistics(){
        return ResponseData.success(service.financeStatistics());
    }

    @GetMapping("updateData")
    @ApiOperation("更新数据")
    public ResponseData updateData(){
        service.updateData();
        return ResponseData.success();
    }

    @GetMapping("listStatistics")
    @ApiOperation("列表页统计")
    public ResponseData listStatistics(){
        return ResponseData.success(service.listStatistics());
    }
}
